package com.runa.rccprotocol.dto.jjn;

import JjnCollectionDecode.DataDecode;
import JjnCollectionDecode.Results;
import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;

import java.util.List;

public class Jjn8124RequestData extends ConcentratorRequestData {

    private byte[] data;

    public Jjn8124RequestData(String address) {
        super(address);
    }

    public void register(List<String[]> equipment) {
        int len = equipment.size() * 12;
        data = new byte[18 + len];
        data[13] = 2;
        int index = 16;
        ByteUtil.shortToByte((short) len, data, 14, ByteOrder.BA);
        for (String [] equ : equipment) {
            if ("".equals(equ[0])) {
                data[index++] = 0x53;
                data[index++] = 0x20;
                data[index++] = 0x20;
            }
            ByteUtil.tenstringToBcd(data, equ[1], index, false);
            index += 9;
        }
        setHaveResult(true);
    }

    public void logout(int num) {
        data = new byte[21];
        data[13] = 4;
        data[14] = 3;
        data[18] = (byte) num;
        setHaveResult(true);
    }

    public void batchReadMeter(int num) {
        data = new byte[20];
        data[13] = 6;
        data[14] = 2;
        data[16] = (byte) num;
        data[17] = (byte) (num >> 8);
        setHaveResult(true);
    }

    public void readMeter(String type, String addr) {
        data = new byte[28];
        data[13] = 7;
        data[14] = 10;
        if ("".equals(type)) {
            data[16] = 0x53;
            data[17] = 0x20;
            data[18] = 0x20;
        }
        ByteUtil.tenstringToBcd(data, addr, 19, false);
        setHaveResult(true);
    }

    public void readValve(String addr) {
        data = new byte[28];
        data[13] = 7;
        data[14] = 10;
        data[16] = 0x53;
        data[17] = 0x33;
        data[18] = 0x20;
        ByteUtil.tenstringToBcd(data, addr, 19, false);
        setHaveResult(true);
    }

    public void setValve(String addr, boolean onOff) {
        data = new byte[30];
        data[13] = 9;
        data[14] = 12;
        data[16] = 0x53;
        data[17] = 0x33;
        data[18] = 0x20;
        ByteUtil.tenstringToBcd(data, addr, 19, false);
        if (onOff) {
            data[26] = 0;
            data[27] = 0x55;
        } else {
            data[26] = 0;
            data[27] = (byte) 0x99;
        }
        setHaveResult(true);
    }

    public void setEnable() {
        data = new byte[22];
        data[13] = 8;
        data[14] = 4;
        data[16] = 0x53;
        data[17] = 0x70;
        data[18] = 0x53;
        data[19] = 0x70;
        setHaveResult(true);
    }

    @Override
    public boolean check(IResponseData data) {
        return true;
    }

    @Override
    public byte[] getData() {
        data[0] = 0x68;
        data[1] = (byte) 0x82;
        ByteUtil.tenstringToBcd(data, address, 2, false);
        data[data.length - 2] = ByteUtil.sum(data, 0, data.length - 2);
        data[data.length - 1] = 0x16;
        DataDecode decode = new DataDecode();
        Results results = decode.dataEncode(ByteUtil.byteToHexString(data));
        if (results.returnResult) {
            data = results.returnData.getBytes();
        } else {
            data = null;
        }
        return data;
    }

    public static void main(String[] args) {
        Jjn8124RequestData requestData = new Jjn8124RequestData("9311534174");

//        requestData.logout(46);
//        System.out.println(newComm String(requestData.getData()));

        requestData.batchReadMeter(10);
        System.out.println(new String(requestData.getData()));

        requestData.readMeter("", "00312922180662");
        System.out.println(new String(requestData.getData()));

        requestData.readValve("00533113320088");
        System.out.println(new String(requestData.getData()));

        requestData.setValve("00533113320088", true);
        System.out.println(new String(requestData.getData()));
//        DataDecode dataDecode = newComm DataDecode();
//        Results rst = dataDecode.dataDecode("6882744153119300000000000041C0003A03301030BD5E1329F50AF5857B904E409BED413B93558611DDB49AC67281F6D7B8F3E8AC872D8A85A38ED3FDBBC8C9641BDF3C6B3CF76938188F3AD7775373FDE3BE4F4EABEC0A613D759FDBCA97E86FBBF9E4978AB0C42CFC47F618A43AB25945DFC3F717B5D509B1C0889D3BBF7D93168E85673F8FEE9654E86CCAEE6A8FC3F566078E11E5A2D60AF5EE8584C60857AD61D9A185D16A818CFC89FD4CB7CF007435AD25D5AB2789DDDADEADE3D5E85C1BDA3272C4FEC003FF8BAA1139C337932D");
//        if (rst.returnResult) {
//            System.out.println(rst.returnData);
//        }
    }
}
